package com.rdgk.newtech.service.impl;

import com.rdgk.newtech.dao.*;
import com.rdgk.newtech.service.NewTechAnalyseService;
import com.rdgk.newtech.service.OrgAnalyseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrgAnalyseServiceImpl implements OrgAnalyseService {
    @Autowired
    private TechniqueArticleDao techArticleDao;
    @Autowired
    private PaperDao paperDao;
    @Autowired
    private PatentDao patentDao;
    @Autowired
    private CraOrganizationDao organizationDao;
    @Autowired
    private TechniqueWordDao techniqueWordDao;
    @Autowired
    private CraAuthorDao authorDao;

    @Override
    public List<Map<String,Object>> importantOrgList(Integer topicId, String[] words) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        return organizationDao.getImportantOrgs(params);
    }

    @Override
    public List<Map<String,Object>> technologyRegionalDistribution(Integer topicId, String[] words) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        return organizationDao.statisticsOrgNumForMap(params);
    }

    @Override
    public Map<String, Object> allOrgCooperateNet(Integer topicId, String keyword) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("keyword",keyword);
        Map<String,Object> results = new HashMap<String,Object>();
        List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();//节点列表
        List<Map<String,Object>> edges = new ArrayList<Map<String,Object>>();//关系列表

        List<Map<String,Object>> techniqueArticleNodes = new ArrayList<Map<String,Object>>();//网络新闻动态节点
        List<Map<String,Object>> paperNodes = new ArrayList<Map<String,Object>>();//论文节点
        List<Map<String,Object>> patentNodes = new ArrayList<Map<String,Object>>();//专利节点

        List<Map<String,Object>> organizationNodes = new ArrayList<Map<String,Object>>();//机构节点

        List<Map<String,Object>> orgTechEdges = new ArrayList<Map<String,Object>>();//机构与网络新闻动态关系

        List<Map<String,Object>> orgPaperEdges = new ArrayList<Map<String,Object>>();//机构与论文关系
        List<Map<String,Object>> orgPatentEdges = new ArrayList<Map<String,Object>>();//机构与专利关系


        List<Map<String,Object>> hotwordArticle = techArticleDao.getArticleByHotword(params);
        List<Object> articleIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合
        for (Map<String,Object> article:hotwordArticle
             ) {
            if(!articleIdParams.contains(article.get("articleid"))){
                Map<String,Object> techniqueArticle = new HashMap<String,Object>();//创建网络动态节点
                techniqueArticle.put("id","tech_"+article.get("articleid"));
                techniqueArticle.put("label",article.get("title"));
                techniqueArticleNodes.add(techniqueArticle);
                articleIdParams.add(article.get("articleid"));//保存每个网络新闻动态的id
            }

        }

        List<Map<String,Object>> hotwordPaper = paperDao.getPaperByHotword(params);
        List<Object> paperIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        for (Map<String,Object> paper:hotwordPaper
                ) {
            if(!paperIdParams.contains(paper.get("paperid"))){
                Map<String,Object> papernode = new HashMap<String,Object>();//创建论文节点
                papernode.put("id","paper_"+paper.get("paperid"));
                papernode.put("label",paper.get("title"));
                paperNodes.add(papernode);
                paperIdParams.add(paper.get("paperid"));
            }
        }

        List<Map<String,Object>> hotwordPatent = patentDao.getPatentByHotword(params);
        List<Object> patentIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        for (Map<String,Object> patent:hotwordPatent
                ) {
            if(!patentIdParams.contains(patent.get("patentid"))){
                Map<String,Object> patendnode = new HashMap<String,Object>();//创建专利节点
                patendnode.put("id","patent_"+patent.get("patentid"));
                patendnode.put("label",patent.get("title"));
                patentNodes.add(patendnode);
                patentIdParams.add(patent.get("patentid"));
            }
        }

        params.put("relArticleList",articleIdParams);
        params.put("relPaperList",paperIdParams);
        params.put("relPatentList",patentIdParams);

        List<Map<String,Object>> relOrgs = organizationDao.getOrgByArticle(params);
        List<Object> tmpOrgIds = new ArrayList<Object>();//临时存放机构id的list，用于去重
        for (Map<String,Object> relorg:relOrgs
             ) {
            Object orgid = relorg.get("orgid");
            if(!tmpOrgIds.contains(orgid)){
                Map<String,Object> orgNode = new HashMap<String,Object>();//创建机构节点
                orgNode.put("id","org_"+relorg.get("orgid"));
                orgNode.put("label",relorg.get("name"));
                organizationNodes.add(orgNode);
                tmpOrgIds.add(orgid);//标记该机构已经出现过
            }
            Map<String,Object> orgArticleEdge = new HashMap<String,Object>();
            orgArticleEdge.put("sourceID","org_"+relorg.get("orgid"));
            orgArticleEdge.put("targetID","tech_"+relorg.get("targetID"));
            orgTechEdges.add(orgArticleEdge);
        }

        List<Map<String,Object>> relOrgPaperPatents = organizationDao.getOrgRelPaperAndPatent(params);
        for (Map<String,Object> relOrgPaperPatent:relOrgPaperPatents
                ) {
            Object orgid = relOrgPaperPatent.get("orgid");
            if(!tmpOrgIds.contains(orgid)){//判断机构id是否已经出现过
                Map<String,Object> orgNode = new HashMap<String,Object>();//创建机构节点
                orgNode.put("id","org_"+orgid);
                orgNode.put("label",relOrgPaperPatent.get("orgname"));
                organizationNodes.add(orgNode);
                tmpOrgIds.add(orgid);//标记该机构已经出现过
            }
            if("1".equals(relOrgPaperPatent.get("reltype"))){//创建专家与论文关系
                Map<String,Object> orgPaperEdge = new HashMap<String,Object>();
                orgPaperEdge.put("sourceID","org_"+orgid);
                orgPaperEdge.put("targetID","paper_"+relOrgPaperPatent.get("targetID"));
                orgPaperEdges.add(orgPaperEdge);
            }
            if("2".equals(relOrgPaperPatent.get("reltype"))) {//创建专家与专利关系{
                Map<String, Object> orgPatentEdge = new HashMap<String, Object>();
                orgPatentEdge.put("sourceID","org_"+orgid);
                orgPatentEdge.put("targetID","patent_"+relOrgPaperPatent.get("targetID"));
                orgPatentEdges.add(orgPatentEdge);
            }
        }

        //将所有节点增加到nodes集合中
        nodes.addAll(organizationNodes);
        nodes.addAll(techniqueArticleNodes);
        nodes.addAll(paperNodes);
        nodes.addAll(patentNodes);
        //将所有关系增加到edges集合中

        edges.addAll(orgTechEdges);
        edges.addAll(orgPaperEdges);
        edges.addAll(orgPatentEdges);
        //将关系和节点列表添加到结果集中
        results.put("nodes",nodes);
        results.put("edges",edges);
        return results;
    }
    @Override
    public  Map<String,Object>  orgDetails(Integer topicId, Integer orgId){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("topicId",topicId);
        params.put("orgId",orgId);
        Map<String,Object> results = new HashMap<String,Object>();

        List<Map<String,Object>> nodes = new ArrayList<Map<String,Object>>();//节点列表
        List<Map<String,Object>> edges = new ArrayList<Map<String,Object>>();//关系列表

        List<Map<String,Object>> orgNodes = new ArrayList<Map<String,Object>>();//网络新闻动态节点
        List<Map<String,Object>> techniqueArticleNodes = new ArrayList<Map<String,Object>>();//网络新闻动态节点
        List<Map<String,Object>> paperNodes = new ArrayList<Map<String,Object>>();//论文节点
        List<Map<String,Object>> patentNodes = new ArrayList<Map<String,Object>>();//专利节点

        List<Map<String,Object>> orgTechEdges = new ArrayList<Map<String,Object>>();//机构与网络新闻动态关系
        List<Map<String,Object>> orgPaperEdges = new ArrayList<Map<String,Object>>();//机构与论文关系
        List<Map<String,Object>> orgPatentEdges = new ArrayList<Map<String,Object>>();//机构与专利关系

        List<Object> articleIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合
        List<Object> paperIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        List<Object> patentIdParams = new ArrayList<Object>();//用于传递参数的论文id的集合
        List<Object> orgIdParams = new ArrayList<Object>();//用于传递参数的网络新闻动态id的集合

        List<Object> orgTechEdgeIds = new ArrayList<Object>();//机构与网络新闻动态关系
        List<Object> orgPaperEdgeIds = new ArrayList<Object>();//机构与论文关系
        List<Object> orgPatentEdgeIds = new ArrayList<Object>();//机构与专利关系

        List<Map<String,Object>> orginfos = organizationDao.getOrgByID(params);
        Map<String,Object> orgdetail = orginfos.size()>0?orginfos.get(0):null;//机构详情
        Map<String,Object> orgNode = new HashMap<String,Object>();
        orgNode.put("id","org_"+orgdetail.get("id"));
        orgNode.put("label",orgdetail.get("NAME"));
        orgNodes.add(orgNode);
        orgIdParams.add(orgdetail.get("id"));

        List<Map<String,Object>> hotworddata = new ArrayList<Map<String,Object>>();//机构相关热词
        hotworddata = organizationDao.getOrgHotwords(params);

        List<Map<String,Object>> authors = new ArrayList<Map<String,Object>>();//机构相关专家
        authors = organizationDao.getOrgAuthors(params);

        List<Map<String,Object>> orgTechEvents = new ArrayList<Map<String,Object>>();//机构相关技术动态

        orgTechEvents = organizationDao.getOrgTechEvents(params);
        for (Map<String,Object> orgTechEvent:orgTechEvents
             ) {
            if("0".equals(orgTechEvent.get("reltype"))){//创建专家与论文关系
                Map<String,Object> echniqueArticleNode = new HashMap<String,Object>();
                if(!articleIdParams.contains(orgTechEvent.get("id"))){
                    echniqueArticleNode.put("id","tech_"+orgTechEvent.get("id"));
                    echniqueArticleNode.put("label",orgTechEvent.get("title"));
                    techniqueArticleNodes.add(echniqueArticleNode);
                    articleIdParams.add(orgTechEvent.get("id"));
                }
                Map<String,Object> orgTechEdge = new HashMap<String,Object>();
                if(!orgTechEdgeIds.contains("org_" + orgdetail.get("id")+"rel"+"tech_" + orgTechEvent.get("id"))) {
                    orgTechEdge.put("sourceID", "org_" + orgdetail.get("id"));
                    orgTechEdge.put("targetID", "tech_" + orgTechEvent.get("id"));
                    orgTechEdges.add(orgTechEdge);
                    orgTechEdgeIds.add("org_" + orgdetail.get("id")+"rel"+"tech_" + orgTechEvent.get("id"));
                }
            }
            if("1".equals(orgTechEvent.get("reltype"))){//创建专家与论文关系
                Map<String,Object> paperNode = new HashMap<String,Object>();
                if(!paperIdParams.contains(orgTechEvent.get("id"))) {
                    paperNode.put("id", "paper_"+orgTechEvent.get("id"));
                    paperNode.put("label", orgTechEvent.get("title"));
                    paperNodes.add(paperNode);
                    paperIdParams.add(orgTechEvent.get("id"));
                }
                Map<String,Object> orgPaperEdge = new HashMap<String,Object>();
                if(!orgPaperEdgeIds.contains("org_"+orgdetail.get("id")+"rel"+"paper_"+orgTechEvent.get("id"))) {
                    orgPaperEdge.put("sourceID","org_"+orgdetail.get("id"));
                    orgPaperEdge.put("targetID","paper_"+orgTechEvent.get("id"));
                    orgPaperEdges.add(orgPaperEdge);
                    orgPaperEdgeIds.add("org_"+orgdetail.get("id")+"rel"+"paper_"+orgTechEvent.get("id"));
                }
            }
            if("2".equals(orgTechEvent.get("reltype"))) {//创建专家与专利关系{
                Map<String, Object> patentNode = new HashMap<String, Object>();
                if(!patentIdParams.contains(orgTechEvent.get("id"))) {
                    patentNode.put("id", "patent_"+orgTechEvent.get("id"));
                    patentNode.put("label", orgTechEvent.get("title"));
                    patentNodes.add(patentNode);
                    patentIdParams.add(orgTechEvent.get("id"));
                }
                Map<String,Object> orgPatentEdge = new HashMap<String,Object>();
                if(!orgPatentEdgeIds.contains( "org_" + orgdetail.get("id")+"rel"+"patent_" + orgTechEvent.get("id"))) {
                    orgPatentEdge.put("sourceID", "org_" + orgdetail.get("id"));
                    orgPatentEdge.put("targetID", "patent_" + orgTechEvent.get("id"));
                    orgPatentEdges.add(orgPatentEdge);
                    orgPatentEdgeIds.add( "org_" + orgdetail.get("id")+"rel"+"patent_" + orgTechEvent.get("id"));
                }
            }
        }

        params.put("relArticleList",articleIdParams);
        params.put("relPaperList",paperIdParams);
        params.put("relPatentList",patentIdParams);
        List<Map<String,Object>> relatedOrgs =  organizationDao.getOrgRelatedOrgs(params);
        for (Map<String,Object> relatedOrg:relatedOrgs
                ) {
            if("0".equals(relatedOrg.get("reltype"))){//创建专家与论文关系
                Map<String,Object> relatedOrgNode = new HashMap<String,Object>();
                if(!orgIdParams.contains(relatedOrg.get("coid"))){
                    relatedOrgNode.put("id","org_"+relatedOrg.get("coid"));
                    relatedOrgNode.put("label",relatedOrg.get("coname"));
                    orgNodes.add(relatedOrgNode);
                    orgIdParams.add(relatedOrg.get("coid"));
                }

                Map<String,Object> orgTechEdge = new HashMap<String,Object>();
                if(!orgTechEdgeIds.contains("org_" + relatedOrg.get("coid")+"rel"+"tech_" + relatedOrg.get("id"))) {
                    orgTechEdge.put("sourceID", "org_" + relatedOrg.get("coid"));
                    orgTechEdge.put("targetID", "tech_" + relatedOrg.get("id"));
                    orgTechEdges.add(orgTechEdge);
                    orgTechEdgeIds.add("org_" + relatedOrg.get("coid")+"rel"+"tech_" + relatedOrg.get("id"));
                }
            }
            if("1".equals(relatedOrg.get("reltype"))){//创建专家与论文关系
                Map<String,Object> relatedOrgNode = new HashMap<String,Object>();
                if(!orgIdParams.contains(relatedOrg.get("coid"))){
                    relatedOrgNode.put("id","org_"+relatedOrg.get("coid"));
                    relatedOrgNode.put("label",relatedOrg.get("coname"));
                    orgNodes.add(relatedOrgNode);
                    orgIdParams.add(relatedOrg.get("coid"));
                }

                Map<String,Object> orgPaperEdge = new HashMap<String,Object>();
                if(!orgPaperEdgeIds.contains("org_" + relatedOrg.get("coid")+"rel"+"paper_" + relatedOrg.get("id"))) {
                    orgPaperEdge.put("sourceID", "org_" + relatedOrg.get("coid"));
                    orgPaperEdge.put("targetID", "paper_" + relatedOrg.get("id"));
                    orgPaperEdges.add(orgPaperEdge);
                    orgPaperEdgeIds.add("org_" + relatedOrg.get("coid")+"rel"+"paper_" + relatedOrg.get("id"));
                }
            }
            if("2".equals(relatedOrg.get("reltype"))) {//创建专家与专利关系{
                Map<String,Object> relatedOrgNode = new HashMap<String,Object>();
                if(!orgIdParams.contains(relatedOrg.get("coid"))) {
                    relatedOrgNode.put("id", "org_"+relatedOrg.get("coid"));
                    relatedOrgNode.put("label", relatedOrg.get("coname"));
                    orgNodes.add(relatedOrgNode);
                    orgIdParams.add(relatedOrg.get("coid"));
                }
                Map<String,Object> orgPatentEdge = new HashMap<String,Object>();
                if(!orgPatentEdgeIds.contains( "org_" + relatedOrg.get("coid")+"rel"+"patent_" + relatedOrg.get("id"))) {
                    orgPatentEdge.put("sourceID", "org_" + relatedOrg.get("coid"));
                    orgPatentEdge.put("targetID", "patent_" + relatedOrg.get("id"));
                    orgPatentEdges.add(orgPatentEdge);
                    orgPatentEdgeIds.add( "org_" + relatedOrg.get("coid")+"rel"+"patent_" + relatedOrg.get("id"));
                }
            }
        }

        //将所有节点增加到nodes集合中
        nodes.addAll(orgNodes);
        nodes.addAll(techniqueArticleNodes);
        nodes.addAll(paperNodes);
        nodes.addAll(patentNodes);
        //将所有关系增加到edges集合中

        edges.addAll(orgTechEdges);
        edges.addAll(orgPaperEdges);
        edges.addAll(orgPatentEdges);
        //将关系和节点列表添加到结果集中
        results.put("nodes",nodes);
        results.put("edges",edges);

        results.put("orgdetail",orgdetail);//机构详细信息
        results.put("hotwords",hotworddata);//机构相关热词
        results.put("authors",authors);//机构相关专家
        results.put("articelNum",articleIdParams.size());//机构相关网络新闻数量
        results.put("paperNum",paperIdParams.size());//机构相关论文数量
        results.put("patentNum",patentIdParams.size());//机构相关论文数量
        return results;
    }
}
